package com.Telien.app.ui.main;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;

import com.Teilen.app.db.main.DBWrapper;
import com.Telien.app.Telien;
import com.Telien.app.request.object.DriverRequest;
import com.Telien.app.request.object.rideService;
import com.Telien.app.user.object.User;
import com.Telien.app.user.service.SessionManagementService;
import com.Telien.app.user.service.UserManagementService;

public class MyRequestsPanel extends JPanel implements ActionListener, TableModelListener {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private User user;
	private UserManagementService ums;
	private SessionManagementService sms;
	private ArrayList<DriverRequest> requests = new ArrayList<DriverRequest>();	
	private ArrayList<Integer> matchingSessionIds = new ArrayList<Integer>();
	private static String columnNames[] = { "Driver Username", "Start Coordinate", "End Coordinate", "Activity","Start Time"};
	Object dataValues[][] = {
			{"", "", "", "", ""}
	};

	private	JTable table;
	private	JScrollPane scrollPane;
	private JPopupMenu popupMenu;
	private JMenuItem requestItem;
	private RequestSearchPanel rsp;
	GridLayout experimentLayout = new GridLayout(3,0);
	private DBWrapper db = new DBWrapper();
	
	
	public MyRequestsPanel(User usr) {
		user = usr;	
		sms = new SessionManagementService();
		ums = new UserManagementService();
		ArrayList<rideService> sessions = sms.getUsersSessions(Integer.parseInt(usr.getUserUUID()));
		for (rideService session : sessions) {
			requests.add(session.getRequest());
			matchingSessionIds.add(session.getrideSessionID());
		}
		if (requests != null) {
			ums.loadUsersFromDb();
		dataValues = new Object [requests.size()][5];
			if (!requests.isEmpty()) {
			//{ "Driver Username", "Start Coordinate", "End Coordinate", "Activity","Start Time"};
				for (int numRequests = 0; numRequests < requests.size(); numRequests++) {					
					dataValues[numRequests][0] = ums.getUserNameMem(requests.get(numRequests).getUserUUID());					
					dataValues[numRequests][1] = requests.get(numRequests).getStartCord();
					dataValues[numRequests][2] = requests.get(numRequests).getEndCord();
					dataValues[numRequests][3] = sms.getRideServiceActivity(matchingSessionIds.get(numRequests));
					dataValues[numRequests][4] = requests.get(numRequests).getStartTime();			
				}
			}
		}
		this.initialize();
	}
	
	public MyRequestsPanel(User usr, ArrayList<DriverRequest> requests, ArrayList<Integer> matchingSessionIds) {
		user = usr;
		ums = new UserManagementService();
		sms = new SessionManagementService();
		//sms.printSessionIds();
		this.requests = requests;
		this.matchingSessionIds = matchingSessionIds;
		if (requests != null) {
			ums.loadUsersFromDb();
		dataValues = new Object [requests.size()][5];
			if (!requests.isEmpty()) {
			//{ "Driver Username", "Start Coordinate", "End Coordinate", "Activity","Start Time"};
				for (int numRequests = 0; numRequests < requests.size(); numRequests++) {					
					dataValues[numRequests][0] = ums.getUserNameMem(requests.get(numRequests).getUserUUID());					
					dataValues[numRequests][1] = requests.get(numRequests).getStartCord();
					dataValues[numRequests][2] = requests.get(numRequests).getEndCord();
					dataValues[numRequests][3] = sms.getRideServiceActivity(matchingSessionIds.get(numRequests));
					dataValues[numRequests][4] = requests.get(numRequests).getStartTime();			
				}
			}
		}
		this.initialize();
	}
	
	private void initialize() {
		setLayout(new BorderLayout());
		table = new JTable(dataValues,columnNames);
		scrollPane = new JScrollPane(table);
		popupMenu = new JPopupMenu();
		requestItem = new JMenuItem("Remove Request");
		requestItem.addActionListener(this);
		popupMenu.add(requestItem);
        table.setComponentPopupMenu(popupMenu);
		add(scrollPane, BorderLayout.CENTER);
				
		setVisible(true);		
	}
	
	public void actionPerformed(ActionEvent e) {
		String name = e.getActionCommand();
		if (name.equals("Remove Request")) {
			System.out.println("Remove Request");
			if (table.getSelectedRow() != -1) {
				db.deleteRideSession(matchingSessionIds.get(table.getSelectedRow()) + "");
				requests.remove(table.getSelectedRow());
				matchingSessionIds.remove(table.getSelectedRow());			
				Telien.getMainFrame().reloadMyRequests();
			}
		}	
	}
	
	public void tableChanged(TableModelEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	
	public ArrayList<DriverRequest> getRequests() {
		return requests;
	}
	
	public void addRequest(DriverRequest request, int rideSessionId) {
		System.out.println("ADDING");
		// if requests already have this request in, don't add it
		if (!requests.contains(request)) {
			requests.add(request);
			matchingSessionIds.add(rideSessionId);
		}
	}

	public ArrayList<Integer> getMatchingSessionIds() {
		return matchingSessionIds;
	}
}
